1
|
|
|
/** |
2
|
|
|
* @file Module 'codingame/api' |
3
|
|
|
* @author woshilapin <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
/** |
6
|
|
|
* API for Codingame's services |
7
|
|
|
* @module codingame/api |
8
|
|
|
*/ |
9
|
|
|
import request from 'request'; |
10
|
|
|
|
11
|
|
|
import cgparse from './parse.js'; |
12
|
|
|
import CodingameError from './error.js'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Codingame's API to log in |
16
|
|
|
* |
17
|
|
|
* @name login |
18
|
|
|
* @function |
19
|
|
|
* @param {string} username - The login to authenticate |
20
|
|
|
* @param {string} password - The password to authenticate |
21
|
|
|
* @returns {Promise<Object>} Body of the response |
22
|
|
|
* @memberof module:codingame/api |
23
|
|
|
* @instance |
24
|
|
|
*/ |
25
|
|
|
let login = function login(username, password) { |
|
|
|
|
26
|
|
|
return new Promise(function(resolve, reject) { |
27
|
|
|
let options = { |
28
|
|
|
"method": `POST`, |
29
|
|
|
"baseUrl": `https://www.codingame.com`, |
30
|
|
|
"uri": `/services/CodingamerRemoteService/loginSiteV2`, |
31
|
|
|
"headers": { |
32
|
|
|
"Accept": `application/json`, |
33
|
|
|
"Content-Type": `application/json;charset=utf-8` |
34
|
|
|
}, |
35
|
|
|
"body": [username, password, true], |
36
|
|
|
"jar": true, |
37
|
|
|
"json": true |
38
|
|
|
}; |
39
|
|
|
request(options, function(error, response, body) { |
40
|
|
|
if (response.statusCode >= 400) { |
41
|
|
|
reject(error); |
42
|
|
|
} else { |
43
|
|
|
resolve(body); |
44
|
|
|
} |
45
|
|
|
}); |
46
|
|
|
}); |
47
|
|
|
}; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Codingame's API for launching a test |
51
|
|
|
* |
52
|
|
|
* @name test |
53
|
|
|
* @function |
54
|
|
|
* @param {string} exercise - Hash of the exercise to test |
55
|
|
|
* @param {number} test - Test's number |
56
|
|
|
* @param {string} language - Language of the program to send |
57
|
|
|
* @param {string} bundle - Content of the program to send |
58
|
|
|
* @returns {Promise<Object>} The result only if request succeed and test passed |
59
|
|
|
* @memberof module:codingame/api |
60
|
|
|
* @instance |
61
|
|
|
*/ |
62
|
|
|
let test = function test(exercise, test, language, bundle) { |
|
|
|
|
63
|
|
|
return new Promise(function(resolve, reject) { |
64
|
|
|
let options = { |
65
|
|
|
"method": `POST`, |
66
|
|
|
"baseUrl": `https://www.codingame.com`, |
67
|
|
|
"uri": `/services/TestSessionRemoteService/play`, |
68
|
|
|
"headers": { |
69
|
|
|
"Accept": `application/json`, |
70
|
|
|
"Content-Type": `application/json;charset=utf-8` |
71
|
|
|
}, |
72
|
|
|
"body": [ |
73
|
|
|
exercise, |
74
|
|
|
{ |
75
|
|
|
"code": bundle, |
76
|
|
|
"programmingLanguageId": language, |
77
|
|
|
"multipleLanguages": { |
78
|
|
|
"testIndex": test |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
], |
82
|
|
|
"jar": true, |
83
|
|
|
"json": true |
84
|
|
|
}; |
85
|
|
|
request(options, function(error, response, body) { |
86
|
|
|
if (response.statusCode >= 400) { |
87
|
|
|
reject(error); |
88
|
|
|
} else { |
89
|
|
|
let meta = { |
90
|
|
|
"exercise": exercise, |
91
|
|
|
"test": test, |
92
|
|
|
"language": language, |
93
|
|
|
"bundle": bundle |
94
|
|
|
}; |
95
|
|
|
cgparse.parse(body) |
96
|
|
|
.then(function(success) { |
|
|
|
|
97
|
|
|
resolve(meta); |
98
|
|
|
}, function(error) { |
99
|
|
|
if (error instanceof CodingameError) { |
100
|
|
|
error.attach(meta); |
101
|
|
|
} |
102
|
|
|
reject(error); |
103
|
|
|
}); |
104
|
|
|
} |
105
|
|
|
}); |
106
|
|
|
}); |
107
|
|
|
}; |
108
|
|
|
|
109
|
|
|
export default { |
110
|
|
|
"login": login, |
111
|
|
|
"test": test |
112
|
|
|
}; |
113
|
|
|
|
This check looks for variables that are declared in multiple lines. There may be several reasons for this.
In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.
If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.